//
//  PwdUpdateViewController.m
//  SelfDriving
//
//  Created by apple on 16/1/14.
//  Copyright © 2016年 apple. All rights reserved.
//

#import "PwdUpdateViewController.h"
#import "BusinessService+WD.h"
@interface PwdUpdateViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (weak, nonatomic) IBOutlet UITableView *table;
@end
@implementation PwdUpdateViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    _table.canCancelContentTouches = YES;
//    _table.cancelsTouchesInView = NO;
}
-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self.view endEditing:YES];
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 1;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row == 0) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell1"];
        //        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell1"];
        return cell;
    }
    if (indexPath.row == 1) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell2"];
        //        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell2"];
        return cell;
    }
    if (indexPath.row == 2) {
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell3"];
        //        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:@"cell3"];
        return cell;
    }
    
    return  nil;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    [self.view endEditing:YES];
}
#pragma mark 保存
- (IBAction)sumbitAction:(UIButton *)sender {
    [self.view endEditing:YES];
    UITextField *t1 = (UITextField *)[self.view viewWithTag:10];
    UITextField *t2 = (UITextField *)[self.view viewWithTag:11];
    UITextField *t3 = (UITextField *)[self.view viewWithTag:12];
    if ([t1.text trim].length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入旧密码"];
        return;
    }
    if ([t1.text trim].length < 6) {
        [SVProgressHUD showErrorWithStatus:@"旧密码必须大于6位"];
        return;
    }
    if ([t2.text trim].length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入新密码"];
        return;
    }
    if ([t2.text trim].length < 6) {
        [SVProgressHUD showErrorWithStatus:@"新密码必须大于6位"];
        return;
    }
    if ([t3.text trim].length == 0) {
        [SVProgressHUD showErrorWithStatus:@"请输入确认新密码"];
        return;
    }
    if ([t3.text trim].length < 6) {
        [SVProgressHUD showErrorWithStatus:@"确认密码必须大于6位"];
        return;
    }
    if (![t2.text isEqualToString:t3.text]) {
        [SVProgressHUD showErrorWithStatus:@"2次密码不一致"];
        return;
    }
    NSMutableDictionary *dic = [[NSMutableDictionary alloc]init];
    [dic setObject:t1.text forKey:@"oldpass"];
    [dic setObject:t3.text forKey:@"newpass"];
    [[BusinessService shareInstance]modifypass:dic success:^(id result) {
        [SVProgressHUD showSuccessWithStatus:@"修改成功"];
        [UserDefaults clearUserDefaultWithKey:@"user"];
        UIStoryboard *customerStoryboard = [UIStoryboard storyboardWithName:@"U"
                                                                     bundle:[NSBundle mainBundle]];
        UIViewController *searchResultDetailController = [customerStoryboard instantiateViewControllerWithIdentifier:@"LoginVC"];
        [self.navigationController pushViewController:searchResultDetailController animated:YES];
    } failure:^(id result) {
        [SVProgressHUD showErrorWithStatus:result];
    }];
    
}
- (IBAction)backActino:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
